import { useAppConfig } from '@vben/hooks';

import { baseUrlApi, requestClient, type Result } from '#/api/request';

export interface ThemesData {
  full_url: string; // 原始网址
  url: string; // 显示网址
  status?: number; // 状态
  id?: number;
}
const { apiURL } = useAppConfig(import.meta.env, import.meta.env.PROD);
/** 请求模板状态 */
export const themesDesign = (data: { status: number; theme: string }) => {
  return requestClient.post<Result>(`${apiURL}/admin/Theme/design`, data);
};

/** 模板列表 */
export const getThemes = () => {
  return requestClient.get<Result>(baseUrlApi('/admin/themes'));
};

/** 未安装模板列表 */
export const getThemesNotInstalled = () => {
  return requestClient.get<Result>(baseUrlApi('/admin/themes/not/installed'));
};

/**
 * 更新模板
 * @param theme
 * @returns
 */
export const putThemes = (theme: string) => {
  return requestClient.put<Result>(baseUrlApi(`/admin/themes/${theme}`));
};

/**
 * 安装模板
 * @param theme
 * @returns
 */
export const postThemes = (theme: string) => {
  return requestClient.post<Result>(baseUrlApi(`/admin/themes/${theme}`));
};

/**
 * 卸载模板
 * @param theme
 * @returns
 */
export const deleteThemes = (theme: string) => {
  return requestClient.delete<Result>(baseUrlApi(`/admin/themes/${theme}`));
};

/**
 * 启用模板
 * @param theme
 * @returns
 */
export const postThemesActive = (theme: string) => {
  return requestClient.post<Result>(
    baseUrlApi(`/admin/themes/${theme}/active`),
  );
};

/**
 * 模板文件列表
 * @param theme
 * @returns
 */
export const getThemesFiles = (theme: string) => {
  return requestClient.get<Result>(baseUrlApi(`/admin/theme/${theme}/files`));
};

/**
 * 获取模板文件设置
 * @param theme
 * @returns
 */
export const getThemesFileSetting = (theme: string, file: string) => {
  return requestClient.get<Result>(
    baseUrlApi(`/admin/theme/${theme}/file/setting`),
    {
      params: { file },
    },
  );
};

/**
 * 模板设置提交
 * @param theme
 * @returns
 */
export const postThemesFileSetting = (theme: string, data: object) => {
  return requestClient.post<Result>(
    baseUrlApi(`/admin/theme/${theme}/file/setting`),
    data,
  );
};

/**
 * 获取模板自由控件设置
 * @param params
 * @returns
 */
export const getThemesWidgetSetting = (params: {
  block_name: string;
  file_id: number;
  widget_id: number;
}) => {
  return requestClient.get<Result>(baseUrlApi(`/admin/theme/widget/setting`), {
    params,
  });
};

/**
 * 模板自由控件设置提交保存
 * @param data
 * @returns
 */
export const postThemesWidgetSetting = (data: {
  admin_content_lang: string;
  block_name: string;
  file_id: number;
  widget?: object;
  widget_id: number | string;
}) => {
  return requestClient.post<Result>(
    baseUrlApi(`/admin/theme/widget/setting`),
    data,
  );
};

/**
 * 自由模板控件排序
 * @param data
 * @returns
 */
export const postThemesWidgetSort = (lang: string, data: object) => {
  return requestClient.post<Result>(
    baseUrlApi(`/admin/theme/widgets/sort?${lang}`),
    data,
  );
};

/**
 * 模板文件块添加控件提交保存
 * @param data
 */
export const postThemesFileBlockWidget = (data: {
  block_name: string;
  file: number | string;
  theme: string;
  widget: string;
}) => {
  return requestClient.post<Result>(
    baseUrlApi(`/admin/theme/file/block/widget`),
    data,
  );
};

/**
 * 模板文件块删除控件
 * @param data
 */
export const deleteThemesFileBlockWidget = (data: {
  admin_content_lang?: string;
  block_name: string;
  file: number | string;
  theme: string;
  widget_id: string;
}) => {
  return requestClient.delete<Result>(
    baseUrlApi(`/admin/theme/file/block/widget`),
    data,
  );
};

/**
 * 获取模板文件模板变量数组数据
 * @param params
 * @returns
 */
export const getThemesFileVarArray = (params: {
  file_id: number;
  var?: string;
}) => {
  return requestClient.get<Result>(baseUrlApi(`/admin/theme/file/var/array`), {
    params,
  });
};

/**
 * 提交模板文件模板变量数组数据
 * @param data
 * @returns
 */
export const postThemesFileVarArray = (data: {
  file_id: number;
  item: object;
  item_index: number | string;
  var?: string;
}) => {
  return requestClient.post<Result>(
    baseUrlApi(`/admin/theme/file/var/array`),
    data,
  );
};

/**
 * 删除模板文件模板变量数组数据
 * @param data
 * @returns
 */
export const deleteThemesFileVarArray = (params: {
  file_id: number;
  item_index: number | string;
  var?: string;
}) => {
  return requestClient.delete<Result>(
    baseUrlApi(`/admin/theme/file/var/array`),
    { params },
  );
};

/**
 * 获取模板文件控件数组数据
 * @param params
 * @returns
 */
export const getThemesFileWidgetArray = (params: {
  file_id: number;
  var: string;
  widget: string;
}) => {
  return requestClient.get<Result>(
    baseUrlApi(`/admin/theme/file/widget/array`),
    { params },
  );
};

/**
 * 模板文件控件数组数据添加编辑提交保存
 * @param data
 * @returns
 */
export const postThemesFileWidgetArray = (data: {
  file_id: number;
  item: object;
  item_index: number;
  var: string;
  widget: string;
}) => {
  return requestClient.put<Result>(
    baseUrlApi(`/admin/theme/file/widget/array`),
    data,
  );
};

/**
 * 删除模板文件控件数组数据
 * @param data
 * @returns
 */
export const deleteThemesFileWidgetArray = (params: {
  file_id: number;
  item_index: number;
  var: string;
  widget: string;
}) => {
  return requestClient.delete<Result>(
    baseUrlApi(`/admin/theme/file/widget/array`),
    { params },
  );
};

/**
 * 获取模板文件块控件数组数据
 * @param data
 * @returns
 */
export const getThemesFileBlockWidgetArray = (params: {
  block_name: string;
  file_id: number;
  var: string;
  widget_id: string;
}) => {
  return requestClient.get<Result>(
    baseUrlApi(`/admin/theme/file/block/widget/array`),
    { params },
  );
};

/**
 * 模板文件块控件数组数据添加编辑提交保存
 * @param data
 * @returns
 */
export const postThemesFileBlockWidgetArray = (data: {
  admin_content_lang?: string;
  block_name: string;
  file_id: number;
  item: object;
  item_index: string;
  var: string;
  widget_id: string;
}) => {
  return requestClient.post<Result>(
    baseUrlApi(`/admin/theme/file/block/widget/array`),
    data,
  );
};

/**
 * 删除模板文件块控件数组数据
 * @param data
 * @returns
 */
export const deleteThemesFileBlockWidgetArray = (data: {
  admin_content_lang?: string;
  block_name: string;
  file_id: number;
  item_index: string;
  var: string;
  widget_id: string;
}) => {
  return requestClient.delete<Result>(
    baseUrlApi(`/admin/theme/file/block/widget/array`),
    data,
  );
};

/**
 * 获取模板文件支持的自由组件列表
 * @param params
 * @returns
 */
export const getThemesFileWidgets = (params: {
  file: string;
  theme: string;
}) => {
  return requestClient.get<Result>(baseUrlApi(`/admin/theme/file/widgets`), {
    params,
  });
};

/**
 * 获取模板文件支持的全局组件列表
 * @param params
 * @returns
 */
export const getThemesFilePublicWidgets = (params: {
  block_name?: string;
  file: string;
  theme: string;
}) => {
  return requestClient.get<Result>(
    baseUrlApi(`admin/theme/file/public/widgets`),
    {
      params,
    },
  );
};
